// pages/deposit/deposit.js


var app = getApp()
const util = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        endTime: "", // 最晚截止时间
        depositDate: "", //存款日期
        tempFilePaths: "", //本地图片路径
        BankList: [],
        BankName: "",
        depositTime:"00:00",
        adding:false //点击存款
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        that.setData({
            endTime: util.formatTime(new Date()).substring(0, 10) // 最晚截止时间
        })
        app.api.request(
            "/BankBaseInfo/GetBankBaseInfo", {},
            "GET",
            res => {
                that.setData({
                    BankList: res.data
                })
            },
            res => {}
        );
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    bindDateChange: function(e) { //存款日期
        this.setData({
            depositDate: e.detail.value
        })
    },
    bindTimeChange(e){
      this.setData({
        depositTime: e.detail.value
      })
    },
    bindPickerChange(e) {
        this.setData({
            index: e.detail.BankId,
            BankName: this.data.BankList[e.detail.value].BankName,
            BankId: this.data.BankList[e.detail.value].BankId
        })
    },
    formSubmit(e) {
      if (this.data.adding){
        return wx.showToast({
          title: '请勿重复存款',
          icon: 'none',
          duration: 2000
        })
      }
        var value = e.detail.value //获取输入form的值
        var ImgBase64 = this.data.tempFilePaths
        value.UserName = app.globalData.openId
        // if (value.DayBookDate == '' || !value.DayBookDate) {
        //   wx.showToast({
        //     title: '存款时间为空',
        //     icon: 'loading',
        //     duration: 2000
        //   })
        //   return
        // }
        if (this.data.BankName == '') {
          wx.showToast({
            title: '存款银行为空',
            icon: 'none',
            duration: 2000
          })
          return
        }
        if (value.InAmount == '') {
          wx.showToast({
            title: '存款金额为空',
            icon: 'none',
            duration: 2000
          })
          return
        }
        if (ImgBase64 == '') {
          wx.showToast({
            title: '存款图片为空',
            icon: 'none',
            duration: 2000
          })
          return
        }
        var that = this
        that.setData({
          adding: true
        })
            // value = JSON.stringify(value)
        wx.uploadFile({
            // http://121.42.252.74:628/api/NewDayBook_BankEntity/Post
            url: 'https://cxfotile.mingm.com.cn/Home/UploadFileNew',
            filePath: ImgBase64,
            name: 'uploadfile_ant',
            header: {
                "Content-Type": "multipart/form-data"
            },
            formData: {
                BankName: this.data.BankName,
                // DayBookDate: value.DayBookDate + value.depositTime,
                Description: value.Description,
                InAmount: value.InAmount,
                UserName: value.UserName
            },
            success: function(res) {
                wx.showToast({
                    title: '存款成功',
                    icon: 'success',
                    duration: 2000
                })
                setTimeout(function() {
                  that.setData({
                    adding:false
                  })
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000)
            },
            fail(res) {
            }
        })
    },

    uploadImage(e) { //获取本地图片地址
        var that = this
        wx.chooseImage({
            success: function(res) {
                var tempFilePaths = res.tempFilePaths
                that.setData({
                    tempFilePaths: tempFilePaths[0]
                })
            }
        })
    },
    clear() {
        this.setData({
            tempFilePaths: ""
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})